/*
 * This class handles all the Twitters events.
 */
package br.com.beltrafficguider.manager;

import br.com.beltrafficguider.model.Tweet;
import br.com.beltrafficguider.model.TwitterUser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

/**
 *
 * @author Carlos Pantoja
 * @since 2011-09-09
 */
public class TwitterManager {

    public TwitterManager() {
        objTwitter = new TwitterFactory().getInstance();
    }

    /**
     * Retrieve all Tweets from the users setted in the configuration file.
     * @return All Tweets
     */
    public List<TwitterUser> findAllTweets() {
        return loadTwitterUsers();
    }

    /**
     * Retrieve the most recent Tweet of the users setted in the configuration 
     * file.
     * @return Latest Tweet 
     */
    public String getLatestTweet() {
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Private Methods">
    private Twitter objTwitter = null;
    private final String PROP_FILE = "twitter-users.properties";
    
    private List<TwitterUser> loadTwitterUsers() {
        List<TwitterUser> twitterUsers = null;

        // Read properties file.
        Properties properties = new Properties();

        try {
            // Read property file
            properties.load(ClassLoader.getSystemResourceAsStream(PROP_FILE));

            // Get user value
            String users = properties.getProperty("users");

            // Check if the tag users were found
            if (users != null) {
                twitterUsers = new ArrayList<TwitterUser>();
                // Fetch all users and add them into the user list
                for (String user : users.split(",")) {
                    TwitterUser twitterUser = new TwitterUser();
                    twitterUser.setName(user);
                    setTweetList(twitterUser);
                    twitterUsers.add(twitterUser);
                }
            } else {
                System.out.println("Could not find the tag users in the property file.");
                System.exit(0);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }

        return twitterUsers;
    }

    private void setTweetList(TwitterUser user) {
        try {
            // The factory instance is re-useable and thread safe.
            List<Status> statuses = objTwitter.getUserTimeline(user.getName());
            
            // Store all the current timelines of twitter users
            List<Tweet> timeLine = new ArrayList<Tweet>();
            for (Status status : statuses) {
                Tweet tweet = new Tweet();
                tweet.setTweet(status.getText());
                tweet.setDate(status.getCreatedAt());
                timeLine.add(tweet);
            }
            
            // Set users timelines
            user.setTweetList(timeLine);
        } catch(TwitterException e) {
            System.out.println(e.getMessage());
            System.exit(0);
        }
    }
    // </editor-fold>
}